package org.electronic.registration.util;

import org.electronic.registration.bo.Record;
import org.electronic.registration.bo.Scheduler;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * User: Egor
 * Date: 09.01.12
 */
public class TransformerImpl {


    public static Record schedulerToRecord(Scheduler scheduler) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(scheduler.getStartDate());
        java.sql.Date recordDate = new java.sql.Date(calendar.getTime().getTime() - calendar.get(Calendar.HOUR) - calendar.get(Calendar.MINUTE));
        Time beginningTime = new Time(scheduler.getStartDate().getTime());
        Time endingDate = new Time(scheduler.getEndDate().getTime());
        return new Record(scheduler.getId(), scheduler.getDoctor(), scheduler.getPatient(), recordDate, beginningTime, endingDate);
    }

    public static Scheduler recordToScheduler(Record record) {
        Calendar calendarDate = new GregorianCalendar();
        Calendar calendarTime = new GregorianCalendar();
        calendarDate.setTime(record.getRecordDate());
        calendarTime.setTime(record.getBeginningTime());
        calendarDate.add(Calendar.HOUR, calendarTime.get(Calendar.HOUR));
        calendarDate.add(Calendar.MINUTE, calendarTime.get(Calendar.MINUTE));
        Date startDate = calendarDate.getTime();

        calendarDate.setTime(record.getRecordDate());
        calendarTime.setTime(record.getEndTime());
        calendarDate.add(Calendar.HOUR, calendarTime.get(Calendar.HOUR));
        calendarDate.add(Calendar.MINUTE, calendarTime.get(Calendar.MINUTE));
        Date endDate = calendarDate.getTime();
        if (record.getPatient() == null) {
            return new Scheduler(record.getId(), startDate, endDate, record.getDoctor(), record.getPatient());
        } else {
            //TODO color and check for patient
            return new Scheduler(record.getId(), startDate, endDate, record.getDoctor(), record.getPatient());
        }

    }

    public static org.apache.commons.collections.Transformer recordToSchedulerTransformer() {
        return new org.apache.commons.collections.Transformer() {
            @Override
            public Scheduler transform(Object input) {
                return recordToScheduler((Record) input);
            }
        };
    }
}
